﻿using MediatR;
using ProductionLine.API.Application.Command.Plan.Write;
using ProductionLine.Domain.Plan;
using ProductionLine.ErrorCode;
using ProductionLine.Infrastructure.Implement;

namespace ProductionLine.API.Application.Handler.Plan.Write
{
    public class DelPlanHandler : IRequestHandler<DelPlanCommand, APIResult<int>>
    {
        DatabaseContext db;

        public DelPlanHandler(DatabaseContext db)
        {
            this.db = db;
        }

        public Task<APIResult<int>> Handle(DelPlanCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = db.SqlSugar.Queryable<PlanModel>().Where(x => x.PlanId == request.PlanId).First();
            res.IsDel = true;
            int r = db.SqlSugar.Updateable(res).ExecuteCommand();
            if (r > 0)
            {
                result.Msg = "删除生产计划成功";
                result.Data = r;
                result.Code = APIEnum.Success;
            }
            else
            {
                result.Msg = "删除生产计划失败";
                result.Data = r;
                result.Code = APIEnum.Fail;
            }
            return Task.FromResult(result);
        }
    }
}
